package com.example.question.bt.backtrack1;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2023-07-16
 * @Author Jonathan
 * @link {<a href="https://leetcode.cn/problems/subsets/">...</a>}
 */
public class Code07_78 {

    public static void main(String[] args) {
        Code07_78 code = new Code07_78();
        System.out.println(code.subsets(new int[]{1, 2, 3}));
    }

    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if (nums == null) {
            return res;
        }
        dfs(nums, 0, new ArrayList<>(), res);
        return res;
    }

    /**
     * 每个节点都有要和不要两种情况
     */
    private void dfs(int[] nums, int startIndex, List<Integer> subset, List<List<Integer>> res) {
        // 第一次进来就将空集加加进来了
        res.add(new ArrayList<>(subset));
        for (int i = startIndex; i < nums.length; i++) {
            subset.add(nums[i]);
            dfs(nums, i + 1, subset, res);
            subset.remove(subset.size() - 1);
        }
    }
}
